<template>
  <div>
    <search-pane
      :show="showSearchPane"
      @search="handleSearchStudent"
      @reset="resetStudentQueryParams"
    >
      <search-item>
        <PDInput
          v-model="queryParams.id"
          input-width="200px"
          description="学号"
        />
      </search-item>
      <search-item>
        <PDInput
          v-model="queryParams.id"
          input-width="200px"
          description="姓名"
        />
      </search-item>
      <search-item>
        <PDInput
          v-model="queryParams.id"
          input-width="200px"
          description="手机号"
        />
      </search-item>

      <search-item
        label="年级"
      >
        <attr-select
          v-model="queryParams.grade"
          attr-name="value"
          type="query"
          @optChange="handleGradeChange"
        >
          <el-option
            v-for="item in gradeOptions"
            :key="item.value"
            :label="item.value"
            :value="item"
          />
        </attr-select>
      </search-item>

      <search-item
        label="学院"
      >
        <attr-select
          v-model="queryParams.college"
          attr-name="value"
          type="form"
          @optChange="handleCollegeChange"
        >
          <el-option
            v-for="item in collegeOptions"
            :key="item.value"
            :label="item.value"
            :value="item"
          />
        </attr-select>
      </search-item>

      <search-item
        label="专业"
      >
        <attr-select
          v-model="queryParams.major"
          attr-name="value"
          type="form"
          @optChange="handleMajorChange"
        >
          <el-option
            v-for="item in majorOptions"
            :key="item.value"
            :label="item.value"
            :value="item"
          />
        </attr-select>
      </search-item>

      <search-item
        label="班级"
      >
        <attr-select
          v-model="queryParams.clazz"
          attr-name="value"
          type="form"
        >
          <el-option
            v-for="item in clazzOptions"
            :key="item.value"
            :label="item.value"
            :value="item"
          />
        </attr-select>
      </search-item>

    </search-pane>

    <table-tool-bar
      v-model="showSearchPane"
      @handleCreate="handleCreateStudent"
      @refresh="fetchStudentPage"
    />

    <table-card>
      <el-table
        ref="filterTable"
        :data="studentList"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          label="学号"
        />
        <el-table-column
          prop="name"
          label="姓名"
        />
        <el-table-column
          prop="grade"
          label="年级"
        />

        <el-table-column
          prop="college"
          label="学院"
        />

        <el-table-column
          prop="major"
          label="专业"
        />

        <el-table-column
          prop="clazz"
          label="班级"
        />

        <el-table-column
          prop="phone"
          label="手机号"
        />

        <el-table-column
          prop="status"
          label="实验权限"
          width="100"
        >
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.status === 1 ? 'primary' : 'success'"
              disable-transitions
            >
              {{ scope.row.status === 1 ? "有权" : "无权" }}
            </el-tag>

          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          fixed="right"
          width="200"
        >
          <template slot-scope="scope">
            <table-option-group
              type="button"
              @handleDelete="handleDeleteStudent(scope.row.id)"
              @handleUpdate="handleUpdateStudent(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="fetchStudentPage"
      />
    </table-card>

    <form-dialog
      v-model="dialogFormVisible"
      :type="formType"
      @create="createStudent"
      @update="updateStudent"
    >
      <el-form
        ref="studentForm"
        :rules="formRules"
        :model="tempStudent"
      >
        <el-form-item prop="id" label="学号">
          <el-input
            v-model="tempStudent.studentId"
            size="mini"
            style="width: 300px"
          />
        </el-form-item>

        <el-form-item prop="name" label="姓名">
          <el-input
            v-model="tempStudent.name"
            size="mini"
            style="width: 300px"
          />
        </el-form-item>

        <el-form-item prop="phone" label="手机号">
          <el-input
            v-model="tempStudent.phone"
            size="mini"
            style="width: 300px"
          />
        </el-form-item>

        <el-form-item prop="grade" label="年级">
          <attr-select
            v-model="tempStudent.grade"
            attr-name="value"
            type="form"
            @optChange="handleGradeChange"
          >
            <el-option
              v-for="item in gradeOptions"
              :key="item.value"
              :label="item.value"
              :value="item"
            />
          </attr-select>
        </el-form-item>

        <el-form-item prop="college" label="学院">

          <attr-select
            v-model="tempStudent.college"
            attr-name="value"
            type="form"
            @optChange="handleCollegeChange"
          >
            <el-option
              v-for="item in collegeOptions"
              :key="item.value"
              :label="item.value"
              :value="item"
            />
          </attr-select>
        </el-form-item>

        <el-form-item prop="major" label="专业">
          <attr-select
            v-model="tempStudent.major"
            attr-name="value"
            type="form"
            @optChange="handleMajorChange"
          >
            <el-option
              v-for="item in majorOptions"
              :key="item.value"
              :label="item.value"
              :value="item"
            />
          </attr-select>
        </el-form-item>

        <el-form-item prop="clazz" label="班级">
          <attr-select
            v-model="tempStudent.clazz"
            attr-name="value"
            type="form"
          >
            <el-option
              v-for="item in clazzOptions"
              :key="item.value"
              :label="item.value"
              :value="item"
            />
          </attr-select>
        </el-form-item>

        <el-form-item prop="state" label="状态">
          <el-radio-group v-model="tempStudent.state" size="mini">
            <el-radio border :label="1">有权</el-radio>
            <el-radio border :label="0">无权</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </form-dialog>
  </div>
</template>

<script>
import SearchPane from '@/components/SearchPane/SearchPane'
import SearchItem from '@/components/SearchPane/SearchItem'
import PDInput from '@/components/SearchPane/PDInput'
import TableCard from '@/components/table/TableCard'
import TableToolBar from '@/components/table/TableToolBar'
import { addStudent, deleteStudent, pageStudents, updateStudent } from '@/api/student'
import FormDialog from '@/components/FormDialog/FormDialog'
import TableOptionGroup from '@/components/table/TableOptionGroup'
import { showSuccessTip } from '@/utils/simple-tips'
import Pagination from '@/components/Pagination'
import { getStuInfoTree } from '@/api/stu-info'
import AttrSelect from '@/components/AttrSelect/AttrSelect'

const GRADE = 'grade'
const COLLEGE = 'college'
const MAJOR = 'major'

const FORM = 'form'
const QUERY = 'query'
export default {
  name: 'Student',
  components: { AttrSelect, Pagination, TableOptionGroup, FormDialog, TableToolBar, TableCard, PDInput, SearchItem, SearchPane },
  data() {
    return {
      // search attr
      showSearchPane: true,
      queryParams: {
        pageSize: 5,
        pageNum: 1,
        grade: undefined,
        college: undefined,
        major: undefined,
        clazz: undefined
      },
      total: 10,

      // form attr
      tempStudent: {},
      dialogFormVisible: false,
      formType: '',
      textMap: {
        update: '修改',
        create: '添加'
      },
      formRules: {},

      // attr value
      stuInfoTree: {},

      gradeOptions: [],
      collegeOptions: [],
      majorOptions: [],
      clazzOptions: [],

      // table attr
      studentList: []
    }
  },
  watch: {

  },
  mounted() {
    this.fetchInfoTree()
    this.fetchStudentPage()
  },
  methods: {

    fetchStudentPage() {
      pageStudents(this.queryParams).then(response => {
        this.studentList = response.data.list
        this.totalCount = response.data.total
      })
    },
    fetchInfoTree() {
      getStuInfoTree().then(response => {
        this.stuInfoTree = response.data
        this.gradeOptions = response.data.children
      })
    },

    // region info tree relative function
    resetTreeOptions(node) {
      if (node === GRADE) {
        this.collegeOptions = []
        this.majorOptions = []
        this.clazzOptions = []
      }
      if (node === COLLEGE) {
        this.majorOptions = []
        this.clazzOptions = []
      }
      if (node === MAJOR) {
        this.clazzOptions = []
      }
    },
    resetTreeBindValue(node, type) {
      if (node === GRADE) {
        if (type === FORM) {
          this.tempStudent.college = undefined
          this.tempStudent.major = undefined
          this.tempStudent.clazz = undefined
        }
        if (type === QUERY) {
          this.queryParams.college = undefined
          this.queryParams.major = undefined
          this.queryParams.clazz = undefined
        }
      }
      if (node === COLLEGE) {
        if (type === FORM) {
          this.tempStudent.major = undefined
          this.tempStudent.clazz = undefined
        }
        if (type === QUERY) {
          this.queryParams.major = undefined
          this.queryParams.clazz = undefined
        }
      }
      if (node === MAJOR) {
        if (type === FORM) {
          this.tempStudent.clazz = undefined
        }
        if (type === QUERY) {
          this.queryParams.clazz = undefined
        }
      }
    },
    handleGradeChange(grade, type) {
      this.resetTreeOptions(GRADE)
      this.resetTreeBindValue(GRADE, type)
      this.collegeOptions = grade.children
    },
    handleCollegeChange(college, type) {
      this.resetTreeOptions(COLLEGE)
      this.resetTreeBindValue(COLLEGE, type)
      this.majorOptions = college.children
    },
    handleMajorChange(major, type) {
      this.resetTreeOptions(MAJOR)
      this.resetTreeBindValue(MAJOR, type)
      this.clazzOptions = major.children
    },
    // endregion

    // region search relative function
    handleSearchStudent() {
      this.fetchStudentPage()
    },
    resetStudentQueryParams() {
      this.resetTreeOptions(GRADE)
      this.resetTreeBindValue(GRADE, QUERY)
      this.queryParams = {
        pageSize: 5,
        pageNum: 1,
        grade: undefined,
        college: undefined,
        major: undefined,
        clazz: undefined
      }
    },
    // endregion

    // region form relative function
    resetTempStudent() {
      this.tempStudent = {}
    },
    handleCreateStudent() {
      this.resetTempStudent()
      this.formType = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['studentForm'].clearValidate()
      })
    },
    createStudent() {
      this.$refs['studentForm'].validate((valid) => {
        if (valid) {
          addStudent(this.tempStudent).then(() => {
            this.fetchStudentPage()
            this.dialogFormVisible = false
            showSuccessTip()
          })
        }
      })
    },
    handleUpdateStudent(row) {
      this.tempStudent = Object.assign({}, row)
      this.formType = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['studentForm'].clearValidate()
      })
    },
    updateStudent() {
      this.$refs['studentForm'].validate((valid) => {
        if (valid) {
          const tempStudent = Object.assign({}, this.tempStudent)
          updateStudent(tempStudent).then(() => {
            this.fetchStudentPage()
            this.dialogFormVisible = false
            showSuccessTip()
          })
        }
      })
    },
    handleDeleteStudent(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteStudent(id).then(() => {
          this.dialogFormVisible = false
          this.fetchStudentPage()
          showSuccessTip()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
    // endregion
  }
}

</script>

<style lang="scss" scoped>
</style>
